package cn.xfdzx.android.apps.shop.presenter;

import android.util.Log;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListMarketBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.NewOrderBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.PaySuccessfulBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.OrderListContract;
import cn.xfdzx.android.apps.shop.contract.PaySuccessfulContract;
import cn.xfdzx.android.apps.shop.model.OrderListModel;
import cn.xfdzx.android.apps.shop.model.PaySuccessfulLikeModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class OrderListPresenter extends BasePresenter<OrderListContract.View> implements OrderListContract.Presenter {

    private OrderListContract.Model model;

    public OrderListPresenter() {
        model = new OrderListModel();
    }


    @Override
    public void initData(boolean isRefresh, boolean isLoading, String type,  String sales_model, String page, String limit) {
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.initData(type,sales_model, page, limit)
                .compose(RxScheduler.<OrderListBeen>Flo_io_main())
                .as(mView.<OrderListBeen>bindAutoDispose())
                .subscribe(new Consumer<OrderListBeen>() {
                    @Override
                    public void accept(OrderListBeen bean) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onOrderListSuccess(bean);
                        } catch (Exception e) {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onError(throwable);
                        } catch (Exception e) {

                        }
                    }
                });
    }

    @Override
    public void toPay(String from, String paytype, String uni, int position) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.toPay(from, paytype, uni)
                .compose(RxScheduler.<NewOrderBeen>Flo_io_main())
                .as(mView.<NewOrderBeen>bindAutoDispose())
                .subscribe(new Consumer<NewOrderBeen>() {
                    @Override
                    public void accept(NewOrderBeen bean) throws Exception {
                        mView.onPaySuccess(bean, position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void confirm(String order_id, int position) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirm(order_id)
                .compose(RxScheduler.<ConfirmBeen>Flo_io_main())
                .as(mView.<ConfirmBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmBeen>() {
                    @Override
                    public void accept(ConfirmBeen bean) throws Exception {
                        mView.onConfirmSuccess(bean, position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getRefundRreason() {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getRefundRreason()
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getRefundRreasonSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterServiceRequest(Map<String, Object> param) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getAfterServiceRequest(param)
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getAfterServiceRequestSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getReturnReason() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getReturnReason()
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getReturnReasonSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterSalesRetailList(boolean isRefresh, boolean isLoading, Map<String, Object> param) {
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.getAfterSalesRetailList(param)
                .compose(RxScheduler.<AfterSalesListRetailBean>Flo_io_main())
                .as(mView.<AfterSalesListRetailBean>bindAutoDispose())
                .subscribe(new Consumer<AfterSalesListRetailBean>() {
                    @Override
                    public void accept(AfterSalesListRetailBean bean) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onGetAfterSalesRetailListSuccess(bean);
                        } catch (Exception e) {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onError(throwable);
                        } catch (Exception e) {

                        }
                    }
                });
    }

    @Override
    public void getIsExceedTime(String orderId) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getIsExceedTime(orderId)
                .compose(RxScheduler.<AfterSalesListRetailBean>Flo_io_main())
                .as(mView.<AfterSalesListRetailBean>bindAutoDispose())
                .subscribe(new Consumer<AfterSalesListRetailBean>() {
                    @Override
                    public void accept(AfterSalesListRetailBean bean) throws Exception {
                        mView.onGetIsExceedTimeSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterSalesMarketList(boolean isRefresh, boolean isLoading, Map<String, Object> param) {
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.getAfterSalesMarketList(param)
                .compose(RxScheduler.<AfterSalesListMarketBean>Flo_io_main())
                .as(mView.<AfterSalesListMarketBean>bindAutoDispose())
                .subscribe(new Consumer<AfterSalesListMarketBean>() {
                    @Override
                    public void accept(AfterSalesListMarketBean bean) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onGetAfterSalesMarketListSuccess(bean);
                        } catch (Exception e) {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        try {
                            if (isLoading) {
                                mView.hideLoading();
                            }
                            if (isRefresh) {
                                mView.finishRefresh();
                            } else {
                                mView.finishLoadMore();
                            }
                            mView.onError(throwable);
                        } catch (Exception e) {

                        }
                    }
                });
    }
}
